// 公用接口
import axios from 'axios'
import request from '@/utils/request'
import { ElMessage } from 'element-plus'
// 分页等可以共用的接口
export function publicApi(url, method, data) {
    if (!url) { ElMessage.warning('请传入url地址'); return }
    return request({
        url,
        // headers: {
        //     isToken: true
        // },
        method: method || 'post',
        data
    })
}
function ossApi(url, method, data) {
    return request({
        url: '/aliyun/oss/generate/post/params',
        method: method || 'post',
        data
    })
}

// export ossApi
function getUUID() {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
        return (c === 'x' ? (Math.random() * 16 | 0) : ('r&0x3' | '0x8')).toString(16)
    })
}
export function uploadOssApi(file,successHandle) {
    ossApi().then((res) => {
        let ossInfo = res.data
        const form = new FormData()
        ossInfo['key'] = res.data.dir + '/' + getUUID() + new Date().getTime()
        ossInfo['ossaccessKeyId'] = res.data.accessId
        ossInfo['success_action_status'] = 200
        for (const key in ossInfo) {
           if(key!='accessId'&&key!='expire'&&key!='expireTime'){
            form.append(key, ossInfo[key])
           }
        }
        form.append('file', file)
        // formData.delete('accessId');
        // formData.delete('expire');
        // formData.delete('expireTime');
        // console.log()
        axios({
            url: ossInfo.host, method: 'post', data: form,
            headers: {
                "Access-Control-Allow-Origin": "*",
                "Content-Type": "multipart/form-data"
            }
        }).then(response => {
            successHandle? successHandle(ossInfo.host+ossInfo.key) : ''
        })
    })
}
